package cn.xxm.strategy.pay.base;

import cn.xxm.strategy.pay.i18n.response.ResponseInfo;
import cn.xxm.strategy.pay.i18n.utils.FI18nUtil;
import lombok.Data;

import java.io.Serializable;

@Data
public class BaseResponse<T> implements Serializable {
    private static final long serialVersionUID = -8469420879227883186L;

    /**
     * 返回码
     */
    private int status = 200;
    /**
     * 消息
     */
    private String msg;
    /**
     * 返回
     */
    private T data;


    public BaseResponse() {

    }

    public BaseResponse(ResultCode resultCode) {
        super();
        this.status = resultCode.code();
        this.msg = resultCode.message();
    }

    public BaseResponse(ResponseInfo resultCode) {
        super();
        this.status = Integer.parseInt(resultCode.getCode()) ;
        this.msg = resultCode.getMsg();
        this.msg = getI18nMsg(resultCode);
    }

    public BaseResponse(String msg, T data) {
        super();
        this.msg = msg;
        this.data = data;
    }

    public BaseResponse(int code, String msg, T data) {
        super();
        this.status = code;
        this.msg = msg;
        this.data = data;
    }


    public static <T> BaseResponse ok(T data) {
        BaseResponse<T> commonResponse = new BaseResponse<>();
        commonResponse.setMsg("success");
        commonResponse.setData(data);
        return commonResponse;
    }

    public static <T> BaseResponse ok() {
        BaseResponse<T> commonResponse = new BaseResponse<>();
        commonResponse.setMsg("success");
        return commonResponse;
    }

    public static <T> BaseResponse error(String msg) {
        BaseResponse<T> commonResponse = new BaseResponse<>();
        commonResponse.setMsg(msg);
        commonResponse.setStatus(9999);
        return commonResponse;
    }

    /**
     * 获取国际化响应信息
     */
    public static String getI18nMsg(ResponseInfo responseInfo) {
        String oriMsg = responseInfo.getMsg();
        String i18nKey = responseInfo.getClass().getSimpleName().replace("ResponseInfo", "").toLowerCase() + "."
                + oriMsg;
        String msg = FI18nUtil.getI18nValue(i18nKey);
        return msg == null ? oriMsg : msg;
    }
}
